import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, OnInit, ViewChild, Input } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { ActivatedRoute } from '@angular/router';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OrganizationUnitDto, ProfileServiceProxy, TaskForModelDto, TaskServiceProxy, WorkflowServiceProxy, UserServiceProxy, OutsourcingFormOutPutDto, OutsourcingWorkFlowServiceProxy, OutsourcingDto, UserEditDto, ExecutionPointerDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { IOrganizationUnitsTreeComponentData, OrganizationUnitsTreeComponent } from '../shared/organization-unit-tree.component';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { TaskService } from '../task/Task.service';
import { accountModuleAnimation } from '@shared/animations/routerTransition';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';

@Component({
  selector: 'OrdinaryOutsourcingDetail',
  templateUrl: './OrdinaryOutsourcingDetail.component.html',
  styleUrls: ['./OrdinaryOutsourcingDetail.component.css']
})
export class OrdinaryOutsourcingDetailComponent  extends AppComponentBase implements OnInit {
  @ViewChild('createOrEditModal') modal: ModalDirective;
  @ViewChild('paginator') paginator: Paginator;
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('recodedataTable') teskdataTable: Table;

  @Input() workflowid: any;
  @Output() out = new EventEmitter();

  active = false;
  saving = false;
  Outsourcings;
  task: TaskForModelDto = new TaskForModelDto();
  workflow;
  detailsprimengTableHelper: PrimengTableHelper;
  constructor(injector: Injector,
    private route: ActivatedRoute,
    private _taskServiceProxy: TaskServiceProxy,
    private _outSourcingworkflowServiceProxy: OutsourcingWorkFlowServiceProxy,
    private _taskService: TaskService,
    private _profileService: ProfileServiceProxy,
    private _userServiceProxy: UserServiceProxy,
    private _workflowService: WorkflowServiceProxy) {
    super(injector);
    this.detailsprimengTableHelper = new PrimengTableHelper();

  }
  ngOnInit(): void {

    console.log(this.workflowid);
    this.initForm();
  }
  initForm(): void {
    this._outSourcingworkflowServiceProxy.getOutsourcingWorkFlow(this.workflowid).subscribe(dto => {
      this.workflow = dto.workFlowDto;
      this.Outsourcings = dto.outSourcingDtos;
      console.log(dto);
      this.primengTableHelper.totalRecordsCount = this.Outsourcings.length;
      this.primengTableHelper.records = this.Outsourcings;
      this.primengTableHelper.hideLoadingIndicator();

      _.forEach(this.Outsourcings, viewItem => {
        console.log(viewItem.taskForModelId);
        this._taskServiceProxy.getTaskForEdit(viewItem.taskForModelId)
          .subscribe(userResult => {
            console.log(userResult);
            viewItem.taskCode = userResult.displayName;
            viewItem.taskName = userResult.code;
            viewItem.picNo = userResult.picNo;
          });

        this.active = true;
      });
    });
  }
}

